/*
 * 创建日期 2010-1-20
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.common.grid.bean;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.common.grid.util.GridConst;
import  org.fantasy.common.util.Constants;
import  org.fantasy.common.util.Parameter;
import  org.fantasy.common.util.Request;
import  org.fantasy.common.util.RequestUtil;
import  org.fantasy.common.util.StringUtil;
import  org.fantasy.common.util.ThreadMap;
import  org.fantasy.common.util.WebUtil;

/**
 *  脚本参数
 * @author: 王文成
 * @version: 1.0
 * @since 2010-1-20
 */
public class ScriptContext {

    private Param param;

    private Request request;

    public ScriptContext(HttpServletRequest request, Param param) {
        this.param = param;
        this.request = RequestUtil.getWrapRequest(request);
    }

    public String getId() {
        return param.getId();
    }
    
    public String getVar() {
        return param.getVar();
    }

    public String getPageCount() {
        String counta = (String)request.getAttribute(GridConst.PAGE_COUNT);
        String countb = request.getParameter(GridConst.PAGE_COUNT);
        return StringUtil.tryThese(new String[] { countb , counta , "0"});
    }

    public String getPageSize() {
        String size = (String) WebUtil.findAttribute(request, GridConst.PAGE_SIZE, "50");
        return request.getString(GridConst.PAGE_SIZE, size);
    }

    public String getPageStart() {
        return request.getString(GridConst.PAGE_START, "1");
    }
    
    public String getRuntimeSql() {
        String sql = (String) ThreadMap.get(Constants.RUNTIME_QUERY);
        return StringUtil.tryThese(new String[]{sql,"select 1 from dual"});
    }
    
    public String getRequestParams() {
        return new Parameter(request).toString();
    }

    public String getPagerId() {
        return param.getVar() + "_" + GridConst.PAGER_DIV;
    }

    public String getTagParamId() {
        return param.getVar() + "_" + GridConst.TAG_PARAM;
    }

    public String getSqlId() {
        return param.getVar() + "_" + GridConst.RUNTIME_SQL;
    }
    
    public String getPagerVar() {
        return param.getVar() + "_" + GridConst.PAGER_VAR;
    }

    public String getRequestParamId() {
        return param.getVar() + "_" + GridConst.REQUEST_PARAMETER;
    }

    public String getAction() {
        return getPath(param.getAction());
    }

    public String getCountAction() {
        return getPath(param.getCountAction());
    }

    private String getPath(String action) {
        String ctx = request.getContextPath();
        if (StringUtil.isValid(action)) {
            if (action.startsWith("/"))
                return ctx + action;
            else
                return ctx + "/" + action;
        }
        return "";
    }

    public int getDataSize() {
        return param.getDataList().size();
    }

    public String getTableConfig() {
        return param.getTableConfig();
    }

    public boolean isPage() {
        return param.isPage();
    }
}
